
const {
    createUser,
    getUserInfo,
    getUserList,
    getUserInfoDetail
} = require('../services/user')

const {
    getFollowInfo
} = require('../services/follow')

const {
    SuccessModel,
    ErrorModel
} = require('../model/ResModel')

const { PAGE_SIZE } = require('../conf/constant')

/**
 * @description 查询用户列表
 * @param {*} param0 
 */
const list = async ({
    province,
    city,
    types,
    pageIndex,
    pageSize = PAGE_SIZE
}) => {
    try {
        const result = await getUserList({
            province,
            city,
            types,
            pageIndex,
            pageSize
        })
        return new SuccessModel(result)
    } catch (err) {
        return new ErrorModel()
    }

    // let sql = `
    //             select id,avatar,username,nickname,cover,type from users where 1=1 
    // `
    // sql += province == '全国' ? '' : `and province=${escape(province)} `
    // sql += city == '全部' ? '' : `and city=${escape(city)} `
    // sql += type == '全部' ? '' : `and type like '%${type}%'; `

    // return listData = await exec(sql)
}

/**
 * @description 用户注册接口
 * @param {*} param0 注册用户相关信息
 */
async function registerUser({
    userName,
    gender,
    avatar,
    openId,
    sessionKey
}) {
    try {
        const data = await createUser({
            userName,
            gender,
            avatar,
            openId,
            sessionKey
        })
        console.log(JSON.stringify(data), 'data1');
        return new SuccessModel({
            id:data.id,
            avatar:data.avatar,
            userName:data.userName
        })
    } catch (er) {

    }
}

/**
 * @description 用户登录获取信息
 * @param {string} id 用户id
 */
async function userLogin(id) {
    const result = await getUserInfo({id})
    console.log(JSON.stringify(result))
    return new SuccessModel(result)
}

/**
 * @description 获取用户详细信息
 * @param {*} id 
 */
async function getUserDetail(id) {
    let userInfo = await getUserInfoDetail(id) //获取用户详细信息
    let followInfo = await getFollowInfo({userId:2,followId:id}) //获取关注关系
    console.log(userInfo);
    console.log(userInfo.dataValues,'dataValues');
    userInfo.dataValues.follow = 0 //默认未关注
    if(followInfo != null ){//判断如果查询到有关注关系
        userInfo.dataValues.follow = 1
    }
    return new SuccessModel(userInfo)
}

module.exports = {
    list,
    registerUser,
    userLogin,
    getUserDetail
}